﻿Imports System.Data.OleDb

Public Class frmXoaPhieuNhapKho
    Public PhieuNhapKhoID As String = -1
    Private Sub frmXoaPhieuNhapKho_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Me.BackColor = Color.FromArgb(194, 217, 247)
        Catch ex As Exception

        End Try
    End Sub


    Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
        Try
            Dim TypeDel As Integer
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim dbCommand As OleDbCommand
            Dim query As String = ""
            dbCommand = New OleDbCommand(query, dbConn)
            If rbtTruKho.Checked Then
                TypeDel = 0
            Else
                TypeDel = 1
            End If
            If TypeDel = 0 Then
                'Tru Kho, Kieu Xoa=1 là có trừ
                PhieuNhapKho_TruKho(PhieuNhapKhoID)
                'Update Xoa=1
                query = "Update PhieuNhapKho Set Xoa=1,KieuXoa=1,ComputerName='" & ComputerName & "',LoginName='" & LoginName & "',NgayXoa=N'" & NgayTao() & "',NguoiXoa='" & NguoiTao() & "'  Where PhieuNhapKhoId=" & PhieuNhapKhoID & ";Update ChiTietPhieuNhapKho Set Xoa=1,NgayXoa=N'" & NgayTao() & "',NguoiXoa='" & NguoiTao() & "'  Where PhieuNhapKhoId=" & PhieuNhapKhoID
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
            Else
                'Update Xoa=1
                query = "Update PhieuNhapKho Set Xoa=1,KieuXoa=0,ComputerName='" & ComputerName & "',LoginName='" & LoginName & "',NgayXoa=N'" & NgayTao() & "',NguoiXoa='" & NguoiTao() & "'  Where PhieuNhapKhoId=" & PhieuNhapKhoID & ";Update ChiTietPhieuNhapKho Set Xoa=1,NgayXoa=N'" & NgayTao() & "',NguoiXoa='" & NguoiTao() & "'  Where PhieuNhapKhoId=" & PhieuNhapKhoID
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
            End If
            Me.Close()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Try
            Me.Close()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub PhieuNhapKho_TruKho(ByVal _PhieuNhapKhoID As String)
        Try
            'kiem tra tinh trang
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim dbCommand As OleDbCommand
            Dim query As String = "Select KhoID From PhieuNhapKho Where PhieuNhapKhoId=" & _PhieuNhapKhoID
            dbCommand = New OleDbCommand(query, dbConn)
            Dim KhoID As String = dbCommand.ExecuteScalar()
            'tru kho
            Dim dtPhieuNhapKho As New DataTable
            query = "Select SoLuongTheoCtu,IDSanPham from ChiTietPhieuNhapKho Where PhieuNhapKhoId=" & _PhieuNhapKhoID
            Dim adapter As OleDb.OleDbDataAdapter
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtPhieuNhapKho)
            Dim SoLuongThuc As Single
            Dim IDSanPham As Integer
            Dim UpDateSoLuongTon As String = ""
            If KhoID = "KhoThanhPham" Then
                UpDateSoLuongTon = "Update SanPham Set SoLuongTon=SoLuongTon - "
            ElseIf KhoID = "KhoXuLy" Then
                UpDateSoLuongTon = "Update SanPham Set SoLuongKhoXuLy=SoLuongKhoXuLy - "
            End If
            For i As Integer = 0 To dtPhieuNhapKho.Rows.Count - 1
                SoLuongThuc = Single.Parse(dtPhieuNhapKho.Rows(i)("SoLuongTheoCtu"))
                IDSanPham = Integer.Parse(dtPhieuNhapKho.Rows(i)("IDSanPham"))
                query = UpDateSoLuongTon & SoLuongThuc & " Where IdSanPham=" & IDSanPham
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
            Next
            dbConn.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
End Class